package com.xms.queryview.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.queryview.entity.QueryView;
import com.xms.queryview.service.IQueryViewFinderService;
import com.xms.queryview.service.IQueryViewUpdaterService;
import com.xms.schema.entity.Entity;
import com.xms.schema.service.entity.IEntityFinderService;
import com.xms.security.core.PrivilegeResourceItem;
import com.xms.utils.CollectionUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import com.xms.web.framework.model.UpdateAuthorizationStateModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * QueryViewAuthorizationController
 * </p>
 *
 * @author migo
 * @since 2021/10/20
 */
@RestController
@RequestMapping("{org}/api/schema/queryview")
public class QueryViewAuthorizationController extends ApiBaseController {
    private IQueryViewFinderService _queryViewFinderService;
    private IQueryViewUpdaterService _queryViewUpdaterService;
    private IEntityFinderService _entityFinderService;

    @Autowired
    public void QueryViewAuthorizationController(IQueryViewFinderService queryViewFinderService,
                                                 IQueryViewUpdaterService queryViewUpdaterService
            , IEntityFinderService entityFinderService) {
        _queryViewFinderService = queryViewFinderService;
        _queryViewUpdaterService = queryViewUpdaterService;
        _entityFinderService = entityFinderService;
    }

    @ApiOperation("查询视图权限资源")
    @GetMapping("privilegeresource")
    public JsonResultObject privilegeResource(@RequestParam(value = "authorizationenabled", required = false) Boolean authorizationEnabled) {
        LambdaQueryWrapper<QueryView> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(QueryView::getStatecode, true)
                .eq(QueryView::getIsdefault, false)
                .eq(QueryView::getIsprivate, false);
        if (authorizationEnabled != null) {
            queryWrapper.eq(QueryView::getAuthorizationenabled, authorizationEnabled);
        }
        queryWrapper.select(QueryView::getQueryviewid, QueryView::getName, QueryView::getEntityid, QueryView::getAuthorizationenabled);
        List<QueryView> data = _queryViewFinderService.list(queryWrapper);
        if (CollectionUtil.notEmpty(data)) {
            List<PrivilegeResourceItem> result = new ArrayList<>();
            List<Entity> entities = _entityFinderService.all();
            for (Entity item : entities) {
                List<QueryView> views = data.stream().filter(x -> x.getEntityid().equals(item.getEntityid())).collect(Collectors.toList());
                if (CollectionUtil.isEmpty(views)) {
                    continue;
                }
                PrivilegeResourceItem group1 = new PrivilegeResourceItem() {
                    {
                        label = item.getLocalizedname();
                        children = views.stream().map(x -> (new PrivilegeResourceItem() {{
                            id = x.getQueryviewid();
                            label = x.getName();
                            authorizationenabled = x.getAuthorizationenabled();
                        }})).collect(Collectors.toList());
                    }
                };
                result.add(group1);
            }
            return JOk(result);
        }
        return JOk();
    }

    @ApiOperation("启用视图权限")
    @PostMapping("authorizationenabled")
    public JsonResultObject authorizationEnabled(@RequestBody UpdateAuthorizationStateModel model) {
        List<QueryView> authorizationViews = _queryViewFinderService.getAuthorizations();
        if (CollectionUtil.notEmpty(authorizationViews)) {
            _queryViewUpdaterService.updateAuthorization(false, authorizationViews.stream().map(x -> x.getQueryviewid()).collect(Collectors.toList()));
        }
        if (CollectionUtil.notEmpty(model.objectId)) {
            _queryViewUpdaterService.updateAuthorization(true, model.objectId);
        }
        return JSaveSuccess();
    }
}
